import { SyncResInfoUtil } from "./SyncResInfoUtil";
import { SyncResExecUtil } from "./SyncResExecUtil";
import { FileChangeUtil } from "./FileChangeUtil";
import { Main } from "../Main";
import { PlistCfgGenUtil } from "./PlistCfgGenUtil";
import { NeArt2CocosStudioUtil } from "./NeArt2CocosStudioUtil";
import { Util } from "./Util";

/**
 * 资源同步工具
 * @author 赵占涛
 */
export class SyncResUtil {
    static export(cb: () => void) {
        this.exportDo("normal", () => {
            // this.exportDo("ios", () => {
            this.exportDo("android", () => {
                PlistCfgGenUtil.gen(() => {
                    FileChangeUtil.saveFileSign(Main.codePath + "Resources/cocosstudio/cocosstudio/", () => {
                        cb();
                    }, SyncResInfoUtil.signOtherCalcSource.bind(SyncResInfoUtil));
                });
            });
            // });
        });
    }
    static exportDo(type: "normal" | "ios" | "android", cb: () => void) {

        let resTmpPath = type == "normal" ? "res/" : (type == "ios" ? "resPlatform/resIos/" : "resPlatform/resAndroid/");
        Util.logTime("开始找变更文件");
        SyncResInfoUtil.getChangeList(type, (needExportList: string[], needDeleteList: string[]) => {
            Util.logTime("结束找变更文件");
            for (let i = 0; i < needExportList.length; i++) {
                console.log("needExportList", i, needExportList[i]);
            }
            for (let i = 0; i < needDeleteList.length; i++) {
                console.log("needDeleteList", i, needDeleteList[i]);
            }
            SyncResExecUtil.exec(type, needExportList, needDeleteList, () => {
                console.log("syncres exec ok");
                FileChangeUtil.saveFileSign(Main.codePath + "Resources/" + resTmpPath, () => {
                    console.log("new md5 saved");
                    cb();
                }, SyncResInfoUtil.signOtherCalcOutput.bind(SyncResInfoUtil))
            });
        });
    }
}